/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: client.c
> Author:cjy
> Mail: 
> Created Time: 2024年08月15日 星期四 11时21分48秒
***************************************************/
// 基于tcp协议开发一个简易c/s模式程序，客户端给服务器发送一个学员信息，服务器收到后显示
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

typedef struct user_info{

	int id;
	char name[10];

}USE;

int main(int argc,const char* argv[])
{
	int sock = 0;
	int ret = 0;

	struct sockaddr_in serAddr;
        memset(&serAddr,0,sizeof(serAddr));

	sock = socket(AF_INET,SOCK_STREAM,0);
	if(sock == -1)
	{
		perror("sock");
		exit(-1);
	}

	serAddr.sin_family = AF_INET;
        serAddr.sin_port = htons(6666);
        serAddr.sin_addr.s_addr = inet_addr("192.168.198.128");

	ret = connect(sock,(struct sockaddr *)&serAddr,sizeof(struct sockaddr));
	if(ret == -1)
	{
		perror("connect");
		exit(-1);
	}

	USE us;
	USE buf;
        while(1)
        {
		printf("please input id:\n");
		scanf("%d",&us.id);
		printf("please input name:\n");
		scanf("%s",us.name);

		ret = send(sock,&us,sizeof(us),0);
		if(ret == -1)
		{
			perror("send");
		}
		ret = recv(sock,&buf,sizeof(buf),0);
		if(ret == -1)
		{
			perror("recv");
			exit(-1);
		}

		printf("id is %d\n",buf.id);
		printf("name is %s\n",buf.name);
 	}

close(sock);

return 0;
}
